home *** CD-ROM | disk | FTP | other *** search
/ AmigActive 2 / AACD 2.iso / AACD / Programming / perlman / man / perlmod.txt < prev    next >
Encoding:
Text File  |  1999-09-09  |  38.9 KB  |  1,006 lines

  1. NAME
  2.        perlmod - Perl modules (packages)
  3.  
  4. DESCRIPTION
  5.        Packages
  6.  
  7.        Perl provides a mechanism for alternative namespaces to
  8.        protect packages from stomping on each others variables.
  9.        In fact, apart from certain magical variables, there's
  10.        really no such thing as a global variable in Perl.  The
  11.        package statement declares the compilation unit as being
  12.        in the given namespace.  The scope of the package
  13.        declaration is from the declaration itself through the end
  14.        of the enclosing block (the same scope as the local()
  15.        operator).  All further unqualified dynamic identifiers
  16.        will be in this namespace.  A package statement only
  17.        affects dynamic variables--including those you've used
  18.        local() on--but not lexical variables created with my().
  19.        Typically it would be the first declaration in a file to
  20.        be included by the require or use operator.  You can
  21.        switch into a package in more than one place; it merely
  22.        influences which symbol table is used by the compiler for
  23.        the rest of that block.  You can refer to variables and
  24.        filehandles in other packages by prefixing the identifier
  25.        with the package name and a double colon:
  26.        $Package::Variable.  If the package name is null, the main
  27.        package as assumed.  That is, $::sail is equivalent to
  28.        $main::sail.
  29.  
  30.        (The old package delimiter was a single quote, but double
  31.        colon is now the preferred delimiter, in part because it's
  32.        more readable to humans, and in part because it's more
  33.        readable to emacs macros.  It also makes C++ programmers
  34.        feel like they know what's going on.)
  35.  
  36.        Packages may be nested inside other packages:
  37.        $OUTER::INNER::var.  This implies nothing about the order
  38.        of name lookups, however.  All symbols are either local to
  39.        the current package, or must be fully qualified from the
  40.        outer package name down.  For instance, there is nowhere
  41.        within package OUTER that $INNER::var refers to
  42.        $OUTER::INNER::var.  It would treat package INNER as a
  43.        totally separate global package.
  44.  
  45.        Only identifiers starting with letters (or underscore) are
  46.        stored in a package's symbol table.  All other symbols are
  47.        kept in package main, including all of the punctuation
  48.        variables like $_.  In addition, the identifiers STDIN,
  49.        STDOUT, STDERR, ARGV, ARGVOUT, ENV, INC and SIG are forced
  50.        to be in package main, even when used for other purposes
  51.        than their built-in one.  Note also that, if you have a
  52.        package called m, s or y, then you can't use the qualified
  53.        form of an identifier because it will be interpreted
  54.        instead as a pattern match, a substitution, or a
  55.        translation.
  56.  
  57.        (Variables beginning with underscore used to be forced
  58.        into package main, but we decided it was more useful for
  59.        package writers to be able to use leading underscore to
  60.        indicate private variables and method names.  $_ is still
  61.        global though.)
  62.  
  63.        Eval()ed strings are compiled in the package in which the
  64.        eval() was compiled.  (Assignments to $SIG{}, however,
  65.        assume the signal handler specified is in the main
  66.        package.  Qualify the signal handler name if you wish to
  67.        have a signal handler in a package.)  For an example,
  68.        examine perldb.pl in the Perl library.  It initially
  69.        switches to the DB package so that the debugger doesn't
  70.        interfere with variables in the script you are trying to
  71.        debug.  At various points, however, it temporarily
  72.        switches back to the main package to evaluate various
  73.        expressions in the context of the main package (or
  74.        wherever you came from).  See the perldebug manpage.
  75.  
  76.        See the perlsub manpage for other scoping issues related
  77.        to my() and local(), or the perlref manpage regarding
  78.        closures.
  79.  
  80.        Symbol Tables
  81.  
  82.        The symbol table for a package happens to be stored in the
  83.        associative array of that name appended with two colons.
  84.        The main symbol table's name is thus %main::, or %:: for
  85.        short.  Likewise the nested package mentioned earlier is
  86.        named %OUTER::INNER::.
  87.  
  88.        The value in each entry of the associative array is what
  89.        you are referring to when you use the *name typeglob
  90.        notation.  In fact, the following have the same effect,
  91.        though the first is more efficient because it does the
  92.        symbol table lookups at compile time:
  93.  
  94.            local(*main::foo) = *main::bar; local($main::{'foo'}) =
  95.            $main::{'bar'};
  96.  
  97.        You can use this to print out all the variables in a
  98.        package, for instance.  Here is dumpvar.pl from the Perl
  99.        library:
  100.  
  101.           package dumpvar;
  102.           sub main::dumpvar {
  103.               ($package) = @_;
  104.               local(*stab) = eval("*${package}::");
  105.               while (($key,$val) = each(%stab)) {
  106.                   local(*entry) = $val;
  107.                   if (defined $entry) {
  108.                       print "\$$key = '$entry'\n";
  109.                   }
  110.  
  111.                   if (defined @entry) {
  112.                       print "\@$key = (\n";
  113.                       foreach $num ($[ .. $#entry) {
  114.                           print "  $num\t'",$entry[$num],"'\n";
  115.                       }
  116.                       print ")\n";
  117.                   }
  118.  
  119.                   if ($key ne "${package}::" && defined %entry) {
  120.                       print "\%$key = (\n";
  121.                       foreach $key (sort keys(%entry)) {
  122.                           print "  $key\t'",$entry{$key},"'\n";
  123.                       }
  124.                       print ")\n";
  125.                   }
  126.               }
  127.           }
  128.  
  129.        Note that even though the subroutine is compiled in
  130.        package dumpvar, the name of the subroutine is qualified
  131.        so that its name is inserted into package main.
  132.  
  133.        Assignment to a typeglob performs an aliasing operation,
  134.        i.e.,
  135.  
  136.            *dick = *richard;
  137.  
  138.        causes variables, subroutines and file handles accessible
  139.        via the identifier richard to also be accessible via the
  140.        symbol dick.  If you only want to alias a particular
  141.        variable or subroutine, you can assign a reference
  142.        instead:
  143.  
  144.            *dick = \$richard;
  145.  
  146.        makes $richard and $dick the same variable, but leaves
  147.        @richard and @dick as separate arrays.  Tricky, eh?
  148.  
  149.        This mechanism may be used to pass and return cheap
  150.        references into or from subroutines if you won't want to
  151.        copy the whole thing.
  152.  
  153.            %some_hash = ();
  154.            *some_hash = fn( \%another_hash );
  155.            sub fn {
  156.                local *hashsym = shift;
  157.                # now use %hashsym normally, and you
  158.                # will affect the caller's %another_hash
  159.                my %nhash = (); # do what you want
  160.                return \%nhash;
  161.            }
  162.  
  163.        On return, the reference wil overwrite the hash slot in
  164.        the symbol table specified by the *some_hash typeglob.
  165.        This is a somewhat tricky way of passing around refernces
  166.        cheaply when you won't want to have to remember to
  167.        dereference variables explicitly.
  168.  
  169.        Another use of symbol tables is for making "constant"
  170.        scalars.
  171.  
  172.            *PI = \3.14159265358979;
  173.  
  174.        Now you cannot alter $PI, which is probably a good thing
  175.        all in all.
  176.  
  177.        Package Constructors and Destructors
  178.  
  179.        There are two special subroutine definitions that function
  180.        as package constructors and destructors.  These are the
  181.        BEGIN and END routines.  The sub is optional for these
  182.        routines.
  183.  
  184.        A BEGIN subroutine is executed as soon as possible, that
  185.        is, the moment it is completely defined, even before the
  186.        rest of the containing file is parsed.  You may have
  187.        multiple BEGIN blocks within a file--they will execute in
  188.        order of definition.  Because a BEGIN block executes
  189.        immediately, it can pull in definitions of subroutines and
  190.        such from other files in time to be visible to the rest of
  191.        the file.
  192.  
  193.        An END subroutine is executed as late as possible, that
  194.        is, when the interpreter is being exited, even if it is
  195.        exiting as a result of a die() function.  (But not if it's
  196.        is being blown out of the water by a signal--you have to
  197.        trap that yourself (if you can).)  You may have multiple
  198.        END blocks within a file--they will execute in reverse
  199.        order of definition; that is: last in, first out (LIFO).
  200.  
  201.        Note that when you use the -n and -p switches to Perl,
  202.        BEGIN and END work just as they do in awk, as a degenerate
  203.        case.
  204.  
  205.        Perl Classes
  206.  
  207.        There is no special class syntax in Perl, but a package
  208.        may function as a class if it provides subroutines that
  209.        function as methods.  Such a package may also derive some
  210.        of its methods from another class package by listing the
  211.        other package name in its @ISA array.
  212.  
  213.        For more on this, see the perlobj manpage.
  214.  
  215.        Perl Modules
  216.  
  217.        A module is just a package that is defined in a library
  218.        file of the same name, and is designed to be reusable.  It
  219.        may do this by providing a mechanism for exporting some of
  220.        its symbols into the symbol table of any package using it.
  221.        Or it may function as a class definition and make its
  222.        semantics available implicitly through method calls on the
  223.        class and its objects, without explicit exportation of any
  224.        symbols.  Or it can do a little of both.
  225.  
  226.        For example, to start a normal module called Fred, create
  227.        a file called Fred.pm and put this at the start of it:
  228.  
  229.            package      Fred;
  230.            require      Exporter;
  231.            @ISA       = qw(Exporter);
  232.            @EXPORT    = qw(func1 func2);
  233.            @EXPORT_OK = qw($sally @listabob %harry func3);
  234.  
  235.        Then go on to declare and use your variables in functions
  236.        without any qualifications.  See the Exporter manpage and
  237.        the Perl Modules File for details on mechanics and style
  238.        issues in module creation.
  239.  
  240.        Perl modules are included into your program by saying
  241.  
  242.            use Module;
  243.  
  244.        or
  245.  
  246.            use Module LIST;
  247.  
  248.        This is exactly equivalent to
  249.  
  250.            BEGIN { require "Module.pm"; import Module; }
  251.  
  252.        or
  253.  
  254.            BEGIN { require "Module.pm"; import Module LIST; }
  255.  
  256.        As a special case
  257.  
  258.            use Module ();
  259.        is exactly equivalent to
  260.  
  261.            BEGIN { require "Module.pm"; }
  262.  
  263.        All Perl module files have the extension .pm.  use assumes
  264.        this so that you don't have to spell out "Module.pm" in
  265.        quotes.  This also helps to differentiate new modules from
  266.        old .pl and .ph files.  Module names are also capitalized
  267.        unless they're functioning as pragmas, "Pragmas" are in
  268.        effect compiler directives, and are sometimes called
  269.        "pragmatic modules" (or even "pragmata" if you're a
  270.        classicist).
  271.  
  272.        Because the use statement implies a BEGIN block, the
  273.        importation of semantics happens at the moment the use
  274.        statement is compiled, before the rest of the file is
  275.        compiled.  This is how it is able to function as a pragma
  276.        mechanism, and also how modules are able to declare
  277.        subroutines that are then visible as list operators for
  278.        the rest of the current file.  This will not work if you
  279.        use require instead of use.  With require you can get into
  280.        this problem:
  281.  
  282.            require Cwd;                # make Cwd:: accessible
  283.            $here = Cwd::getcwd();
  284.  
  285.            use Cwd;                    # import names from Cwd::
  286.            $here = getcwd();
  287.  
  288.            require Cwd;                # make Cwd:: accessible
  289.            $here = getcwd();           # oops! no main::getcwd()
  290.  
  291.        In general use Module (); is recommended over require
  292.        Module;.
  293.  
  294.        Perl packages may be nested inside other package names, so
  295.        we can have package names containing ::.  But if we used
  296.        that package name directly as a filename it would makes
  297.        for unwieldy or impossible filenames on some systems.
  298.        Therefore, if a module's name is, say, Text::Soundex, then
  299.        its definition is actually found in the library file
  300.        Text/Soundex.pm.
  301.  
  302.        Perl modules always have a .pm file, but there may also be
  303.        dynamically linked executables or autoloaded subroutine
  304.        definitions associated with the module.  If so, these will
  305.        be entirely transparent to the user of the module.  It is
  306.        the responsibility of the .pm file to load (or arrange to
  307.        autoload) any additional functionality.  The POSIX module
  308.        happens to do both dynamic loading and autoloading, but
  309.        the user can just say use POSIX to get it all.
  310.  
  311.        For more information on writing extension modules, see the
  312.        perlxs manpage and the perlguts manpage.
  313. NOTE
  314.        Perl does not enforce private and public parts of its
  315.        modules as you may have been used to in other languages
  316.        like C++, Ada, or Modula-17.  Perl doesn't have an
  317.        infatuation with enforced privacy.  It would prefer that
  318.        you stayed out of its living room because you weren't
  319.        invited, not because it has a shotgun.
  320.  
  321.        The module and its user have a contract, part of which is
  322.        common law, and part of which is "written".  Part of the
  323.        common law contract is that a module doesn't pollute any
  324.        namespace it wasn't asked to.  The written contract for
  325.        the module (AKA documentation) may make other provisions.
  326.        But then you know when you use RedefineTheWorld that
  327.        you're redefining the world and willing to take the
  328.        consequences.
  329.  
  330. THE PERL MODULE LIBRARY
  331.        A number of modules are included the the Perl
  332.        distribution.  These are described below, and all end in
  333.        .pm.  You may also discover files in the library directory
  334.        that end in either .pl or .ph.  These are old libraries
  335.        supplied so that old programs that use them still run.
  336.        The the .ph files made by h2ph will probably end up as
  337.        extension modules made by h2xs.  (Some .ph values may
  338.        already be available through the POSIX module.)  The pl2pm
  339.        file in the distribution may help in your conversion, but
  340.        it's just a mechanical process, so is far from bullet
  341.        proof.
  342.  
  343.        Pragmatic Modules
  344.  
  345.        They work somewhat like pragmas in that they tend to
  346.        affect the compilation of your program, and thus will
  347.        usually only work well when used within a use, or no.
  348.        These are locally scoped, so an inner BLOCK may
  349.        countermand any of these by saying
  350.  
  351.            no integer;
  352.            no strict 'refs';
  353.  
  354.        which lasts until the end of that BLOCK.
  355.  
  356.        The following programs are defined (and have their own
  357.        documentation).
  358.  
  359.        diagnostics Pragma to produce enhanced diagnostics
  360.  
  361.        integer     Pragma to compute arithmetic in integer
  362.                    instead of double
  363.  
  364.        less        Pragma to request less of something from the
  365.                    compiler
  366.  
  367.        overload    Pragma for overloading operators
  368.  
  369.        sigtrap     Pragma to enable stack backtrace on unexpected
  370.                    signals
  371.  
  372.        strict      Pragma to restrict unsafe constructs
  373.  
  374.        subs        Pragma to predeclare sub names
  375.  
  376.        Standard Modules
  377.  
  378.        Standard, bundled modules are all expected to behave in a
  379.        well-defined manner with respect to namespace pollution
  380.        because they use the Exporter module.  See their own
  381.        documentation for details.
  382.  
  383.        AnyDBM_File provide framework for multiple DBMs
  384.  
  385.        AutoLoader  load functions only on demand
  386.  
  387.        AutoSplit   split a package for autoloading
  388.  
  389.        Benchmark   benchmark running times of code
  390.  
  391.        Carp        warn of errors (from perspective of caller)
  392.  
  393.        Config      access Perl configuration option
  394.  
  395.        Cwd         get pathname of current working directory
  396.  
  397.        DB_File     Perl access to Berkeley DB
  398.  
  399.        Devel::SelfStubber
  400.                    generate stubs for a SelfLoading module
  401.  
  402.        DynaLoader  Dynamically load C libraries into Perl code
  403.  
  404.        English     use nice English (or awk) names for ugly
  405.                    punctuation variables
  406.  
  407.        Env         perl module that imports environment variables
  408.  
  409.        Exporter    provide inport/export controls for Perl
  410.                    modules
  411.  
  412.        ExtUtils::Liblist
  413.                    determine libraries to use and how to use them
  414.  
  415.        ExtUtils::MakeMaker
  416.                    create an extension Makefile
  417.  
  418.        ExtUtils::Manifest
  419.                    utilities to write and check a MANIFEST file
  420.  
  421.        ExtUtils::Mkbootstrap
  422.                    make a bootstrap file for use by DynaLoader
  423.  
  424.        ExtUtils::Miniperl
  425.                    !!!GOOD QUESTION!!!
  426.  
  427.        Fcntl       load the C Fcntl.h defines
  428.  
  429.        File::Basename
  430.                    parse file specifications
  431.  
  432.        File::CheckTree
  433.                    run many filetest checks on a tree
  434.  
  435.        File::Find  traverse a file tree
  436.  
  437.        FileHandle  supply object methods for filehandles
  438.  
  439.        File::Path  create or remove a series of directories
  440.  
  441.        Getopt::Long
  442.                    extended getopt processing
  443.  
  444.        Getopt::Std Process single-character switches with switch
  445.                    clustering
  446.  
  447.        I18N::Collate
  448.                    compare 8-bit scalar data according to the
  449.                    current locale
  450.  
  451.        IPC::Open2  a process for both reading and writing
  452.  
  453.        IPC::Open3  open a process for reading, writing, and error
  454.                    handling
  455.  
  456.        Net::Ping   check a host for upness
  457.  
  458.        POSIX       Perl interface to IEEE Std 1003.1
  459.  
  460.        SelfLoader  load functions only on demand
  461.  
  462.        Safe        Creation controlled compartments in which perl
  463.                    code can be evaluated.
  464.  
  465.        Socket      load the C socket.h defines and structure
  466.                    manipulators
  467.  
  468.        Test::Harness
  469.                    run perl standard test scripts with statistics
  470.  
  471.        Text::Abbrev
  472.                    rceate an abbreviation table from a list
  473.  
  474.        To find out all the modules installed on your system,
  475.        including those without documentation or outside the
  476.        standard release, do this:
  477.  
  478.            find `perl -e 'print "@INC"'` -name '*.pm' -print
  479.  
  480.        They should all have their own documentation installed and
  481.        accessible via your system man(1) command.  If that fails,
  482.        try the perldoc program.
  483.  
  484.        Extension Modules
  485.  
  486.        Extension modules are written in C (or a mix of Perl and
  487.        C) and get dynamically loaded into Perl if and when you
  488.        need them.  Supported extension modules include the
  489.        Socket, Fcntl, and POSIX modules.
  490.  
  491.        Many popular C extension modules do not come bundled (at
  492.        least, not completely) due to their size, volatility, or
  493.        simply lack of time for adequate testing and configuration
  494.        across the multitude of platforms on which Perl was beta-
  495.        tested.  You are encouraged to look for them in
  496.        archie(1L), the Perl FAQ or Meta-FAQ, the WWW page, and
  497.        even with their authors before randomly posting asking for
  498.        their present condition and disposition.
  499.  
  500. CPAN
  501.        CPAN stands for the Comprehensive Perl Archive Network.
  502.        This is a globally replicated collection of all known Perl
  503.        materials, including hundreds of unbunded modules.  Here
  504.        are the major categories of modules:
  505.  
  506.        o Language Extensions and Documentation Tools
  507.  
  508.        o Development Support
  509.  
  510.        o Operating System Interfaces
  511.  
  512.        o Networking, Device Control (modems) and InterProcess
  513.             Communication
  514.  
  515.        o Data Types and Data Type Utilities
  516.  
  517.        o Database Interfaces
  518.  
  519.        o User Interfaces
  520.  
  521.        o Interfaces to / Emulations of Other Programming
  522.             Languages
  523.  
  524.        o File Names, File Systems and File Locking (see also File
  525.             Handles)
  526.  
  527.        o String Processing, Language Text Processing, Parsing and
  528.             Searching
  529.        o Option, Argument, Parameter and Configuration File
  530.             Processing
  531.  
  532.        o Internationalization and Locale
  533.  
  534.        o Authentication, Security and Encryption
  535.  
  536.        o World Wide Web, HTML, HTTP, CGI, MIME
  537.  
  538.        o Server and Daemon Utilities
  539.  
  540.        o Archiving and Compression
  541.  
  542.        o Images, Pixmap and Bitmap Manipulation, Drawing and
  543.             Graphing
  544.  
  545.        o Mail and Usenet News
  546.  
  547.        o Control Flow Utilities (callbacks and exceptions etc)
  548.  
  549.        o File Handle and Input/Output Stream Utilities
  550.  
  551.        o Miscellaneous Modules
  552.  
  553.        Some of the reguster CPAN sites as of this writing include
  554.        the following.  You should try to choose one close to you:
  555.  
  556.        o ftp://ftp.sterling.com/programming/languages/perl/
  557.  
  558.        o ftp://ftp.sedl.org/pub/mirrors/CPAN/
  559.  
  560.        o ftp://ftp.uoknor.edu/mirrors/CPAN/
  561.  
  562.        o ftp://ftp.delphi.com/pub/mirrors/packages/perl/CPAN/
  563.  
  564.        o ftp://uiarchive.cso.uiuc.edu/pub/lang/perl/CPAN/
  565.  
  566.        o ftp://ftp.cis.ufl.edu/pub/perl/CPAN/
  567.  
  568.        o ftp://ftp.switch.ch/mirror/CPAN/
  569.  
  570.        o ftp://ftp.sunet.se/pub/lang/perl/CPAN/
  571.  
  572.        o ftp://ftp.ci.uminho.pt/pub/lang/perl/
  573.  
  574.        o ftp://ftp.cs.ruu.nl/pub/PERL/CPAN/
  575.  
  576.        o ftp://ftp.demon.co.uk/pub/mirrors/perl/CPAN/
  577.  
  578.        o ftp://ftp.rz.ruhr-uni-
  579.             bochum.de/pub/programming/languages/perl/CPAN/
  580.  
  581.        o
  582.             ftp://ftp.leo.org/pub/comp/programming/languages/perl/CPAN/
  583.        o ftp://ftp.pasteur.fr/pub/computing/unix/perl/CPAN/
  584.  
  585.        o ftp://ftp.ibp.fr/pub/perl/CPAN/
  586.  
  587.        o ftp://ftp.funet.fi/pub/languages/perl/CPAN/
  588.  
  589.        o ftp://ftp.tekotago.ac.nz/pub/perl/CPAN/
  590.  
  591.        o ftp://ftp.mame.mu.oz.au/pub/perl/CPAN/
  592.  
  593.        o ftp://coombs.anu.edu.au/pub/perl/
  594.  
  595.        o ftp://dongpo.math.ncu.edu.tw/perl/CPAN/
  596.  
  597.        o ftp://ftp.lab.kdd.co.jp/lang/perl/CPAN/
  598.  
  599.        o ftp://ftp.is.co.za/programming/perl/CPAN/
  600.  
  601.        For an up-to-date listing of CPAN sites, see
  602.        http://www.perl.com/perl/ or ftp://ftp.perl.com/perl/ .
  603.  
  604. Modules: Creation, Use and Abuse
  605.        (The following section is borrowed directly from Tim
  606.        Bunce's modules file, available at your nearest CPAN
  607.        site.)
  608.  
  609.        Perl 5 implements a class using a package, but the
  610.        presence of a package doesn't imply the presence of a
  611.        class.  A package is just a namespace.  A class is a
  612.        package that provides subroutines that can be used as
  613.        methods.  A method is just a subroutine that expects, as
  614.        its first argument, either the name of a package (for
  615.        "static" methods), or a reference to something (for
  616.        "virtual" methods).
  617.  
  618.        A module is a file that (by convention) provides a class
  619.        of the same name (sans the .pm), plus an import method in
  620.        that class that can be called to fetch exported symbols.
  621.        This module may implement some of its methods by loading
  622.        dynamic C or C++ objects, but that should be totally
  623.        transparent to the user of the module.  Likewise, the
  624.        module might set up an AUTOLOAD function to slurp in
  625.        subroutine definitions on demand, but this is also
  626.        transparent.  Only the .pm file is required to exist.
  627.  
  628.        Guidelines for Module Creation
  629.  
  630.        Do similar modules already exist in some form?
  631.            If so, please try to reuse the existing modules either
  632.            in whole or by inheriting useful features into a new
  633.            class.  If this is not practical try to get together
  634.            with the module authors to work on extending or
  635.            enhancing the functionality of the existing modules.
  636.            A perfect example is the plethora of packages in perl4
  637.            for dealing with command line options.
  638.  
  639.            If you are writing a module to expand an already
  640.            existing set of modules, please coordinate with the
  641.            author of the package.  It helps if you follow the
  642.            same naming scheme and module interaction scheme as
  643.            the original author.
  644.  
  645.        Try to design the new module to be easy to extend and
  646.            reuse.
  647.            Use blessed references.  Use the two argument form of
  648.            bless to bless into the class name given as the first
  649.            parameter of the constructor, e.g.:
  650.  
  651.             sub new {
  652.                    my $class = shift;
  653.                    return bless {}, $class;
  654.             }
  655.  
  656.            or even this if you'd like it to be used as either a
  657.            static or a virtual method.
  658.  
  659.             sub new {
  660.                    my $self  = shift;
  661.                    my $class = ref($self) || $self;
  662.                    return bless {}, $class;
  663.             }
  664.  
  665.            Pass arrays as references so more parameters can be
  666.            added later (it's also faster).  Convert functions
  667.            into methods where appropriate.  Split large methods
  668.            into smaller more flexible ones.  Inherit methods from
  669.            other modules if appropriate.
  670.  
  671.            Avoid class name tests like: die "Invalid" unless ref
  672.            $ref eq 'FOO'.  Generally you can delete the "eq
  673.            'FOO'" part with no harm at all.  Let the objects look
  674.            after themselves! Generally, avoid hardwired class
  675.            names as far as possible.
  676.  
  677.            Avoid $r->Class::func() where using @ISA=qw(... Class
  678.            ...) and $r->func() would work (see perlbot man page
  679.            for more details).
  680.  
  681.            Use autosplit so little used or newly added functions
  682.            won't be a burden to programs which don't use them.
  683.            Add test functions to the module after __END__ either
  684.            using AutoSplit or by saying:
  685.  
  686.             eval join('',<main::DATA>) || die $@ unless caller();
  687.  
  688.            Does your module pass the 'empty sub-class' test? If
  689.            you say "@SUBCLASS::ISA = qw(YOURCLASS);" your
  690.            applications should be able to use SUBCLASS in exactly
  691.            the same way as YOURCLASS.  For example, does your
  692.            application still work if you change:  $obj = new
  693.            YOURCLASS; into: $obj = new SUBCLASS; ?
  694.  
  695.            Avoid keeping any state information in your packages.
  696.            It makes it difficult for multiple other packages to
  697.            use yours. Keep state information in objects.
  698.  
  699.            Always use -w. Try to use strict; (or use strict
  700.            qw(...);).  Remember that you can add no strict
  701.            qw(...); to individual blocks of code which need less
  702.            strictness. Always use -w. Always use -w!  Follow the
  703.            guidelines in the perlstyle(1) manual.
  704.  
  705.        Some simple style guidelines
  706.            The perlstyle manual supplied with perl has many
  707.            helpful points.
  708.  
  709.            Coding style is a matter of personal taste. Many
  710.            people evolve their style over several years as they
  711.            learn what helps them write and maintain good code.
  712.            Here's one set of assorted suggestions that seem to be
  713.            widely used by experienced developers:
  714.  
  715.            Use underscores to separate words.  It is generally
  716.            easier to read $var_names_like_this than
  717.            $VarNamesLikeThis, especially for non-native speakers
  718.            of English. It's also a simple rule that works
  719.            consistently with VAR_NAMES_LIKE_THIS.
  720.  
  721.            Package/Module names are an exception to this rule.
  722.            Perl informally reserves lowercase module names for
  723.            'pragma' modules like integer and strict. Other
  724.            modules normally begin with a capital letter and use
  725.            mixed case with no underscores (need to be short and
  726.            portable).
  727.  
  728.            You may find it helpful to use letter case to indicate
  729.            the scope or nature of a variable. For example:
  730.  
  731.             $ALL_CAPS_HERE   constants only (beware clashes with perl vars)
  732.             $Some_Caps_Here  package-wide global/static
  733.             $no_caps_here    function scope my() or local() variables
  734.  
  735.            Function and method names seem to work best as all
  736.            lowercase.  E.g., $obj->as_string().
  737.  
  738.            You can use a leading underscore to indicate that a
  739.            variable or function should not be used outside the
  740.            package that defined it.
  741.  
  742.        Select what to export.
  743.            Do NOT export method names!
  744.            Do NOT export anything else by default without a good
  745.            reason!
  746.  
  747.            Exports pollute the namespace of the module user.  If
  748.            you must export try to use @EXPORT_OK in preference to
  749.            @EXPORT and avoid short or common names to reduce the
  750.            risk of name clashes.
  751.  
  752.            Generally anything not exported is still accessible
  753.            from outside the module using the
  754.            ModuleName::item_name (or $blessed_ref->method)
  755.            syntax.  By convention you can use a leading
  756.            underscore on names to informally indicate that they
  757.            are 'internal' and not for public use.
  758.  
  759.            (It is actually possible to get private functions by
  760.            saying: my $subref = sub { ... };  &$subref; But
  761.            there's no way to call that directly as a method,
  762.            since a method must have a name in the symbol table.)
  763.  
  764.            As a general rule, if the module is trying to be
  765.            object oriented then export nothing. If it's just a
  766.            collection of functions then @EXPORT_OK anything but
  767.            use @EXPORT with caution.
  768.  
  769.        Select a name for the module.
  770.            This name should be as descriptive, accurate and
  771.            complete as possible.  Avoid any risk of ambiguity.
  772.            Always try to use two or more whole words.  Generally
  773.            the name should reflect what is special about what the
  774.            module does rather than how it does it.  Please use
  775.            nested module names to informally group or categorise
  776.            a module.  A module should have a very good reason not
  777.            to have a nested name.  Module names should begin with
  778.            a capital letter.
  779.  
  780.            Having 57 modules all called Sort will not make life
  781.            easy for anyone (though having 23 called Sort::Quick
  782.            is only marginally better :-).  Imagine someone trying
  783.            to install your module alongside many others.  If in
  784.            any doubt ask for suggestions in comp.lang.perl.misc.
  785.  
  786.            If you are developing a suite of related
  787.            modules/classes it's good practice to use nested
  788.            classes with a common prefix as this will avoid
  789.            namespace clashes. For example:  Xyz::Control,
  790.            Xyz::View, Xyz::Model etc. Use the modules in this
  791.            list as a naming guide.
  792.  
  793.            If adding a new module to a set, follow the original
  794.            author's standards for naming modules and the
  795.            interface to methods in those modules.
  796.  
  797.            To be portable each component of a module name should
  798.            be limited to 11 characters. If it might be used on
  799.            DOS then try to ensure each is unique in the first 8
  800.            characters. Nested modules make this easier.
  801.  
  802.        Have you got it right?
  803.            How do you know that you've made the right decisions?
  804.            Have you picked an interface design that will cause
  805.            problems later? Have you picked the most appropriate
  806.            name? Do you have any questions?
  807.  
  808.            The best way to know for sure, and pick up many
  809.            helpful suggestions, is to ask someone who knows.
  810.            Comp.lang.perl.misc is read by just about all the
  811.            people who develop modules and it's the best place to
  812.            ask.
  813.  
  814.            All you need to do is post a short summary of the
  815.            module, its purpose and interfaces. A few lines on
  816.            each of the main methods is probably enough. (If you
  817.            post the whole module it might be ignored by busy
  818.            people - generally the very people you want to read
  819.            it!)
  820.  
  821.            Don't worry about posting if you can't say when the
  822.            module will be ready - just say so in the message. It
  823.            might be worth inviting others to help you, they may
  824.            be able to complete it for you!
  825.  
  826.        README and other Additional Files.
  827.            It's well known that software developers usually fully
  828.            document the software they write. If, however, the
  829.            world is in urgent need of your software and there is
  830.            not enough time to write the full documentation please
  831.            at least provide a README file containing:
  832.  
  833.        o A description of the module/package/extension etc.
  834.  
  835.        o A copyright notice - see below.
  836.  
  837.        o Prerequisites - what else you may need to have.
  838.  
  839.        o How to build it - possible changes to Makefile.PL etc.
  840.  
  841.        o How to install it.
  842.  
  843.        o Recent changes in this release, especially
  844.                      incompatibilities
  845.  
  846.        o Changes / enhancements you plan to make in the future.
  847.  
  848.                      If the README file seems to be getting too
  849.                      large you may wish to split out some of the
  850.                      sections into separate files: INSTALL,
  851.                      Copying, ToDo etc.
  852.        Adding a Copyright Notice.
  853.            How you choose to licence your work is a personal
  854.            decision.  The general mechanism is to assert your
  855.            Copyright and then make a declaration of how others
  856.            may copy/use/modify your work.
  857.  
  858.            Perl, for example, is supplied with two types of
  859.            licence: The GNU GPL and The Artistic License (see the
  860.            files README, Copying and Artistic).  Larry has good
  861.            reasons for NOT just using the GNU GPL.
  862.  
  863.            My personal recommendation, out of respect for Larry,
  864.            Perl and the perl community at large is to simply
  865.            state something like:
  866.  
  867.             Copyright (c) 1995 Your Name. All rights reserved.
  868.             This program is free software; you can redistribute it and/or
  869.             modify it under the same terms as Perl itself.
  870.  
  871.            This statement should at least appear in the README
  872.            file. You may also wish to include it in a Copying
  873.            file and your source files.  Remember to include the
  874.            other words in addition to the Copyright.
  875.  
  876.        Give the module a version/issue/release number.
  877.            To be fully compatible with the Exporter and MakeMaker
  878.            modules you should store your module's version number
  879.            in a non-my package variable called $VERSION.  This
  880.            should be a valid floating point number with at least
  881.            two digits after the decimal (ie hundredths, e.g,
  882.            $VERSION = "0.01").  Don't use a "1.3.2" style
  883.            version.  See Exporter.pm in Perl5.001m or later for
  884.            details.
  885.  
  886.            It may be handy to add a function or method to
  887.            retrieve the number.  Use the number in announcements
  888.            and archive file names when releasing the module
  889.            (ModuleName-1.02.tar.Z).  See perldoc
  890.            ExtUtils::MakeMaker.pm for details.
  891.  
  892.        How to release and distribute a module.
  893.            It's good idea to post an announcement of the
  894.            availability of your module (or the module itself if
  895.            small) to the comp.lang.perl.announce Usenet
  896.            newsgroup.  This will at least ensure very wide once-
  897.            off distribution.
  898.  
  899.            If possible you should place the module into a major
  900.            ftp archive and include details of it's location in
  901.            your announcement.
  902.  
  903.            Some notes about ftp archives: Please use a long
  904.            descriptive file name which includes the version
  905.            number. Most incoming directories will not be
  906.            readable/listable, i.e., you won't be able to see your
  907.            file after uploading it. Remember to send your email
  908.            notification message as soon as possible after
  909.            uploading else your file may get deleted
  910.            automatically. Allow time for the file to be processed
  911.            and/or check the file has been processed before
  912.            announcing its location.
  913.  
  914.            FTP Archives for Perl Modules:
  915.  
  916.            Follow the instructions and links on
  917.  
  918.               http://franz.ww.tu-berlin.de/modulelist
  919.  
  920.            or upload to one of these sites:
  921.  
  922.               ftp://franz.ww.tu-berlin.de/incoming
  923.               ftp://ftp.cis.ufl.edu/incoming
  924.  
  925.            and notify upload@franz.ww.tu-berlin.de.
  926.  
  927.            By using the WWW interface you can ask the Upload
  928.            Server to mirror your modules from your ftp or WWW
  929.            site into your own directory on CPAN!
  930.  
  931.            Please remember to send me an updated entry for the
  932.            Module list!
  933.  
  934.        Take care when changing a released module.
  935.            Always strive to remain compatible with previous
  936.            released versions (see 2.2 above) Otherwise try to add
  937.            a mechanism to revert to the old behaviour if people
  938.            rely on it. Document incompatible changes.
  939.  
  940.        Guidelines for Converting Perl 4 Library Scripts into
  941.        Modules
  942.  
  943.        There is no requirement to convert anything.
  944.            If it ain't broke, don't fix it! Perl 4 library
  945.            scripts should continue to work with no problems. You
  946.            may need to make some minor changes (like escaping
  947.            non-array @'s in double quoted strings) but there is
  948.            no need to convert a .pl file into a Module for just
  949.            that.
  950.  
  951.        Consider the implications.
  952.            All the perl applications which make use of the script
  953.            will need to be changed (slightly) if the script is
  954.            converted into a module.  Is it worth it unless you
  955.            plan to make other changes at the same time?
  956.  
  957.        Make the most of the opportunity.
  958.            If you are going to convert the script to a module you
  959.            can use the opportunity to redesign the interface. The
  960.            'Guidelines for Module Creation' above include many of
  961.            the issues you should consider.
  962.  
  963.        The pl2pm utility will get you started.
  964.            This utility will read *.pl files (given as
  965.            parameters) and write corresponding *.pm files. The
  966.            pl2pm utilities does the following:
  967.  
  968.        o Adds the standard Module prologue lines
  969.  
  970.        o Converts package specifiers from ' to ::
  971.  
  972.        o Converts die(...) to croak(...)
  973.  
  974.        o Several other minor changes
  975.  
  976.                      Being a mechanical process pl2pm is not
  977.                      bullet proof. The converted code will need
  978.                      careful checking, especially any package
  979.                      statements.  Don't delete the original .pl
  980.                      file till the new .pm one works!
  981.  
  982.        Guidelines for Reusing Application Code
  983.  
  984.        Complete applications rarely belong in the Perl Module
  985.            Library.
  986.  
  987.        Many applications contain some perl code which could be
  988.            reused.
  989.            Help save the world! Share your code in a form that
  990.            makes it easy to reuse.
  991.  
  992.        Break-out the reusable code into one or more separate
  993.            module files.
  994.  
  995.        Take the opportunity to reconsider and redesign the
  996.            interfaces.
  997.  
  998.        In some cases the 'application' can then be reduced to a
  999.            small
  1000.            fragment of code built on top of the reusable modules.
  1001.            In these cases the application could invoked as:
  1002.  
  1003.                 perl -e 'use Module::Name; method(@ARGV)' ...
  1004.            or
  1005.                 perl -mModule::Name ...    (in perl5.002?)
  1006.